473,425 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

populating a drop down using another drop down

27
hii guyzz
Im doing a project which requires that i have a drop down for the country and i require to populate another drop down for state.
Oct 31 '06 #1
6 12147
hii guyzz
Im doing a project which requires that i have a drop down for the country and i require to populate another drop down for state.
Hi,

You can try this code snippet. I may be helpful.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Country Selection</title>
  4. <script language="javascript">
  5. var Country = null;
  6. function init()
  7. {
  8.     var numCountry = 2;
  9.     var oStateSlct = document.CountrySelect.Function;
  10.     Country = new Array(numCountry+1); 
  11.     oStateSlct.options[0] = new Option("Please select a Country Type", "-1");
  12.     Country[0] = new Array(0); 
  13.     oStateSlct.options[1] = new Option("Country1", "1");
  14.     Country[1] = new Array(1);
  15.     Country[1][0] = new Option("State1", "-1");
  16.     oStateSlct.options[2] = new Option("Country2", "2");
  17.     Country[2] = new Array(2);
  18.     Country[2][0] = new Option("State1", "-1");
  19.     Country[2][1] = new Option("State2", "0");
  20.  
  21. }
  22.  
  23. function fillUpSelectControl(srcCntrl,targetCntrl,selectOptions)
  24. {
  25.     var slctIndx = srcCntrl.options.selectedIndex;
  26.     for( i=0; i<targetCntrl.options.length; i++ )
  27.         targetCntrl.options[i] = null;
  28.  
  29.     var newOptions = selectOptions[slctIndx]; 
  30.     for( i=0; i<newOptions.length; i++ )
  31.         targetCntrl.options[i] = new Option( newOptions[i].text, newOptions[i].value );
  32.     targetCntrl.options[0].selected = true;
  33. }
  34. </script>
  35. </head>
  36. <body onload="init();">
  37.  
  38. <form name="CountrySelect" >
  39. <select NAME="Function" onChange="fillUpSelectControl(this,this.form.StateType,Country)">
  40. </select>
  41. <select name="StateType">
  42. </select>
  43. </form>                
  44.  
  45. </body> 
  46. </html>
Nov 1 '06 #2
spooky
27
hii
thanks for the snippet , i've been doin this project using Servlets,JSP and JDBC.
I dont know much about scripting how can i connect to db using this
anil
Nov 1 '06 #3
iam_clint
1,208 Expert 1GB
You would probably have to use AJAX, DOM you can find help for that all over google good examples.
Nov 1 '06 #4
spooky
27
Will this code work if yes can i get the value of hidden Name to my jsp page!!


function madeSelection(selObj)
{
var selectedValue = selObj.options[selObj.selectedIndex].value;
//document.Name.value = "ANIL";
document.hiddenName.value = selectedValue;
document.write(document.hiddenName.value);
}
html tag------
input type=hidden name=hiddenName
Nov 3 '06 #5
r035198x
13,262 8TB
Will this code work if yes can i get the value of hidden Name to my jsp page!!


function madeSelection(selObj)
{
var selectedValue = selObj.options[selObj.selectedIndex].value;
//document.Name.value = "ANIL";
document.hiddenName.value = selectedValue;
document.write(document.hiddenName.value);
}
html tag------
input type=hidden name=hiddenName
No. This will not work.
The culprit line is
Expand|Select|Wrap|Line Numbers
  1.  document.hiddenName.value = selectedValue;
which should be
Expand|Select|Wrap|Line Numbers
  1.  document.formName.hiddenName.value = selectedValue;
or
Expand|Select|Wrap|Line Numbers
  1.  
  2. document.forms['formName'].hiddenName.value = selectedValue;
  3.  
Where formName is the name of the form on which the hidden input is.
Yes you can get to this value in a jsp using
Expand|Select|Wrap|Line Numbers
  1.  String name = request.getParameter("hiddenName");
Nov 3 '06 #6
Hi ,
ur code is really excellent. can u just give me
the code by taking the values of countries and states from the mysql db. is it possible to take the values from the db..?(mysql and jsp). can pls reply back immediately....thanks in advance..
Sep 16 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Deb | last post by:
I am having a problem creating a form in Access 2000 which I have done with no problem in 97. I have an unbound drop down box. This idea is for the user to select from the drop down box and the...
1
by: msnews.microsoft.com | last post by:
I'd like to hear your thoughts on best methods for populating drop down list controls. I have states and countries drop down lists that don't change often, so naturally I "hard code" them in the...
1
by: asad | last post by:
Hello, i want to populate the drop down box values based on the first drop down box value, actually i have form in which user will select a car maker and based on that value below 2 drop down...
4
by: Pete Lux | last post by:
I have a drop down that populates on page load. The drop down brings in customer numbers from my local MSDE database. It does this fine, but I click a button that finds quotes for those customers...
2
by: Jim in Arizona | last post by:
My goal, somehow, is to populate a dropdownlist with all the user names in active directory. I don't even know where to begin, really. I added a reference to System.DirectoryServices so I could...
6
by: Rob Meade | last post by:
Hi all, Looking for a bit of help if possible. For about 2 weeks now I've been investigating the best way to populate related drop down menus, and have their values pre-populated again if the...
11
by: tokcy | last post by:
Hi everyone, I am new in php and ajax, i am facing the prob while i click on element of first drop down then in second dropdown all element showl come from database. I mean i have three dropdown 1....
3
by: DaveRook | last post by:
Hi I am using C# and Access databse for a web site. I have a drop down list which I need to populate dynamically according to results in a database. Example There will be 1 drop down list...
14
by: Philth | last post by:
Hi there, I've essentially got a form with several drop down, each populated by columns in various tables. The populating bit works fine - the column rows appear as they should in the menu. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.